package com.power.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2019/01/10 9:53
 * Description:
 * Version: V1.0
 */
public class MD5 {
        /**
         *  MD5 加密为32位的唯一标识符
         * @param source
         * @return
         * @throws NoSuchAlgorithmException
         */
        public static StringBuffer getMD5(String source) throws NoSuchAlgorithmException {
            //将明文转换成byte[]
            byte [] bytes = source.getBytes();
            //将byte字节数组通过MessageDigest进行MD5运算，得到一个新的字节数组。
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte [] newBytes = md.digest(bytes);
            //将新的字节数组转换成密文
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < newBytes.length; i++) {
                sb.append(String.format("%02X", newBytes[i]));
            }
            return sb;
        }

}
